﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Emoney.Cms.Spider;
using Emoney.Spider.Basic;
using System.Data.SqlClient;
using DAL;

public partial class Spider_NewsEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int ID = OTO.ToInt(Request["ID"], 0);
            if (ID > 0)
            {
                CmsAccess cAcc = new CmsAccess();
                CmsEnty et = cAcc.NewsContentByID(ID);
                txtTitle.Text = et.TITLE;
                txtAuthor.Text = et.AUTHOR;
                txtSource.Text = et.SOURCE;
                txtKey.Text = et.KEYWORD;
                txtSummary.Text = et.SUMMARY;
                txtContent.Text = et.CONTENT;
                txtPTime.Text = OTO.ToDate(et.PTIME, DateTime.Now).ToString();
                hidID.Value = ID.ToString();
                hidCID.Value = et.CID.ToString();

            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            CmsEnty et = new CmsEnty();
            et.ID = OTO.ToInt(hidID.Value, 0);
            et.CID = OTO.ToInt(hidCID.Value, 0);
            if (et.ID > 0 && et.CID > 0)
            {
                et.TITLE = txtTitle.Text;
                et.AUTHOR = txtAuthor.Text;
                et.SOURCE = txtSource.Text;
                et.KEYWORD = txtKey.Text;
                et.SUMMARY = txtSummary.Text;
                et.CONTENT = txtContent.Text;
                et.PTIME = OTO.ToDate(txtPTime.Text, DateTime.Now);
                et.CTIME = DateTime.Now;
                et.STOCKCODE = txtStock.Text;
                int _enable = chkEnable.Checked == true ? 1 : 0;
                et.FLAG = _enable == 1 ? 1 : 0;

                SqlParameter[] para = new SqlParameter[10];
                para[0] = new SqlParameter("@ID", et.ID);
                para[1] = new SqlParameter("@TITLE", et.TITLE);
                para[2] = new SqlParameter("@AUTHOR", et.AUTHOR);
                para[3] = new SqlParameter("@SOURCE", et.SOURCE);
                para[4] = new SqlParameter("@KEYWORD", et.KEYWORD);
                para[5] = new SqlParameter("@SUMMARY", et.SUMMARY);
                para[6] = new SqlParameter("@CONTENT", et.CONTENT);
                para[7] = new SqlParameter("@PTIME", et.PTIME);
                para[8] = new SqlParameter("@STOCK", et.STOCKCODE);
                para[9] = new SqlParameter("@FLAG", et.FLAG);
                //para[8] = new SqlParameter("@REl",System.Data.SqlDbType.Int);
                //para[8].Direction = System.Data.ParameterDirection.Output;
                int err;
                //修改采集到的信息
                err = SQLHelper.ExecuteNonQuery(SQLHelper.StrConn, System.Data.CommandType.StoredProcedure, "sp_UpdateNews2", para);

                if (_enable == 1)
                {
                    SqlParameter[] para2 = new SqlParameter[12];
                    para2[0] = new SqlParameter("@AUTHOR", et.AUTHOR);
                    para2[1] = new SqlParameter("@CTIME", et.CTIME);
                    para2[2] = new SqlParameter("@PTIME", et.PTIME);
                    para2[3] = new SqlParameter("@TITLE", et.TITLE);
                    para2[4] = new SqlParameter("@KEYWORD", et.KEYWORD);
                    para2[5] = new SqlParameter("@SUMMARY", et.SUMMARY);
                    para2[6] = new SqlParameter("@CONTENT", et.CONTENT);
                    para2[7] = new SqlParameter("@STEP", 1);
                    para2[8] = new SqlParameter("@ISENABLE", 1);
                    para2[9] = new SqlParameter("@STOCK", et.STOCKCODE);
                    para2[10] = new SqlParameter("@SOURCE", et.SOURCE);
                    para2[11] = new SqlParameter("@REl", System.Data.SqlDbType.BigInt);
                    para2[11].Direction = System.Data.ParameterDirection.Output;

                    int flag;
                    flag = SQLHelper.ExecuteNonQuery(SQLHelper.StrConn2, System.Data.CommandType.StoredProcedure, "sp_SpiderToNews", para2);
                    var oid = para2[11].Value;

                    if (flag == 1)
                    {
                        SqlParameter[] para3 = new SqlParameter[1];
                        para3[0] = new SqlParameter("@StrID", et.ID.ToString());
                        SQLHelper.ExecuteNonQuery(SQLHelper.StrConn, System.Data.CommandType.StoredProcedure, "sp_NewsPost", para3);
                    }

                }
            }

            string key = string.Format(CacheKey.News_CACHE_KEY, "DB", et.CID.ToString());
            Cache.Remove(key);
            Response.Write("<script>alert('更新成功');this.location.href='NewsList.aspx';</script>");
            Response.End();
        }
        catch
        { }
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        int _ID = OTO.ToInt(hidID.Value, 0);
        Response.Redirect("NewsList.aspx?ID=" + _ID + "&ac=del");
        Response.End();
    }
}